package com.qzsf.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzsf.backend.common.Result;
import com.qzsf.backend.dto.NotificationDTO;
import com.qzsf.backend.entity.Registration;
import com.qzsf.backend.mapper.EventMapper;
import com.qzsf.backend.mapper.RegistrationMapper;
import com.qzsf.backend.service.RegistrationService;
import com.qzsf.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/21 9:40 周四
 * @Email 1429278462@qq.com
 * @Remark 报名服务层实现类
 * @Version 1.0
 */
@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private EventMapper eventMapper;

    @Override
    public Result getRegistration(Integer status, String userName, String eventName,int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return Result.success(new PageInfo<>(registrationMapper.getRegistration(status, userName, eventName)));
    }

    @Override
    @Transactional
    public Result batchApprove(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("审批列表不能为空");
        }
        // 根据报名 ID 获取所有对应的 event_id
        List<Integer> eventIds = registrationMapper.getEventIdsByRegistrationIds(ids);
        List<Integer> userIds = registrationMapper.getUserIdsByRegistrationIds(ids);
        // 校验是否存在超额情况
        for (Integer eventId : eventIds) {
            // 获取活动的 remaining 数量
            int remaining = eventMapper.getRemainingByEventId(eventId);

            // 获取当前已审批人数（status = 1）
            int approvedCount = registrationMapper.countApprovedByEventId(eventId);

            // 校验是否超过
            if (ids.size() + approvedCount > remaining) {
                String eventName = eventMapper.getEventById(eventId).getName();
                return Result.error("活动ID " + eventId + "活动名称"+ eventName+ " 的报名人数超过剩余可用名额！");
            }
        }
        // 校验通过后，进行批量审批
        registrationMapper.batchApprove(ids);
        return Result.success();
    }

    @Override
    public Result batchReject(List<Integer> ids) {
        registrationMapper.batchReject(ids);
        return Result.success();
    }

    @Override
    public Result createRegistration(int eventId) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        Registration registration = new Registration(id,eventId,0);
        if(registrationMapper.createRegistration(registration)>0){
            return Result.success();
        }else{
            return Result.error("创建报名信息失败");
        }
    }

    @Override
    public Result getEventRegisteredUser(int eventId) {
        return Result.success(registrationMapper.getEventRegisteredUser(eventId));
    }

    @Override
    public Result getEventUncheckedRegistration(int eventId) {
        return Result.success(registrationMapper.getEventUncheckedRegistration(eventId));
    }

    @Override
    public Result cancelRegistration(int eventId) {
        Map<String,Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("id");
        if(registrationMapper.cancelRegistration(eventId,userId)>0){
            return Result.success();
        }else{
            return Result.error("取消报名失败");
        }
    }
}
